package moo.stargalaxy_bungee.api;

import com.alibaba.fastjson.JSONObject;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import moo.stargalaxy_bungee.GalaxyBunGee;
import moo.stargalaxy_bungee.config.Config;
import moo.stargalaxy_bungee.data.StarGalaxyData;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.api.CommandSender;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class GalaxyBungeeAPI {
    public static boolean addTpCoolDown(final ProxiedPlayer proxiedPlayer){
        if(StarGalaxyData.getCoolDownMap().containsKey(proxiedPlayer.getUniqueId())){
            proxiedPlayer.sendMessage("§2请勿频繁转换子服");
            return false;
        } else {
            StarGalaxyData.getCoolDownMap().put(proxiedPlayer.getUniqueId(), true);
            GalaxyBunGee.getPlugin().getProxy().getScheduler().schedule(GalaxyBunGee.getPlugin(), new Runnable() {
                @Override
                public void run() {
                    StarGalaxyData.getCoolDownMap().remove(proxiedPlayer.getUniqueId());
                }
            }, Config.getConfiguration().getInt("BunGee_Set.GoServer_Delay"), TimeUnit.SECONDS);
            return true;
        }
    }

    // /fun_crossserverfid_normal?server_fid=<服务器识别码>
    public static void tpServer(final  ProxiedPlayer proxiedPlayer, String server){
        JSONObject crossServerFid = GalaxyAPI.getCrossServerFid(server);
        if(crossServerFid.getInteger("code") == 1 && crossServerFid.getString("msg").equals("Success") && crossServerFid.containsKey("data")){
            JSONObject data = crossServerFid.getJSONObject("data");
            if(!GalaxyBunGee.getPlugin().getProxy().getServers().containsKey(server)){
                InetSocketAddress address = new InetSocketAddress(data.getString("连接地址"), data.getInteger("分配端口"));
                ServerInfo serverInfo = ProxyServer.getInstance().constructServerInfo(server, address, "From Plugin StarGalaxy-Bungee", false);
                StarGalaxyData.getServerInfoMap().put(server, serverInfo);
            }
            GalaxyBungeeAPI.tpServer(proxiedPlayer, server);
        }
    }

    public static String intiGalaxyAPI(){
        GalaxyAPI.url = "http://" +  Config.getConfiguration().getString("StarGalaxy_Link.IP") + ":" + Config.getConfiguration().getString( "StarGalaxy_Link.PORT");
        GalaxyAPI.key = Config.getConfiguration().getString("StarGalaxy_Link.SECRETKEY");
        try {
            JSONObject serverTemplates = GalaxyAPI.getServerTemplates();
            if(serverTemplates.getString("msg").equals("Success")){
                // GalaxyBunGee.getPlugin().getLogger().info("§f┃    §8初始化：§a控制台已连接");
                return "§a控制台连接成功";
            } else {
                // GalaxyBunGee.getPlugin().getLogger().info("§f┃    §8初始化：§c控制台连接失败 §8(无法解析响应数据)");
                return "§c控制台连接失败 §8(无法解析响应数据)";
            }
        } catch (Exception e) {
            // GalaxyBunGee.getPlugin().getLogger().info("§f┃    §8初始化：§c控制台连接失败 §8(连接地址或端口错误)");
            return "§c控制台连接失败 §8(连接地址或端口错误)";
        }
    }

    public static void sendMessage(String server) throws IOException {
        sendCustomData("Login", "test", 1);
        //sendCustomData("server1", "test", 1);
    }

    public static void sendCustomData(String server, String data1, int data2)
    {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        out.writeUTF( "MySubChannel" ); // the channel could be whatever you want
        out.writeUTF( data1 ); // this data could be whatever you want
        out.writeInt( data2 ); // this data could be whatever you want

        // we send the data to the server
        // using ServerInfo the packet is being queued if there are no players in the server
        // using only the server to send data the packet will be lost if no players are in it
        ServerInfo serverInfo = GalaxyBunGee.getPlugin().getProxy().getServerInfo(server);
        System.out.println(serverInfo.getMotd());
        GalaxyBunGee.getPlugin().getProxy().getServerInfo(server).sendData( "stargalaxy:message", out.toByteArray() );
    }

}

